﻿/*
 *  
 * Responds to: http://x.x.x.x/sensor/1 by turning onboard sensor on.
 * http://x.x.x.x/sensor/0 turns it off.
 * 
 */

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusController
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        static string receivedStr = "";
        static string responseStr = "";

        public static void Main()
        {
            CommonNetduino.tcpCaller tcpCaller1 = new CommonNetduino.tcpCaller(
                80, "192.168.1.141", 5, true, false);
            tcpCaller1.MessageReceived += new CommonNetduino.MessageReceivedEventHandler(tcpCaller1_MessageReceived);
            //tcpCaller1.call();

            while (true)
            {

                if (tcpCaller1.isConnected)
                {
                    Debug.Print("server is connected at " + DateTime.Now.ToString());
                }
                else
                {
                    Debug.Print("server NOT connected at " + DateTime.Now.ToString());
                }
                Thread.Sleep(1000);
                led.Write(!led.Read());
            }
        }

        static void tcpCaller1_MessageReceived(object sender, CommonNetduino.MessageEventArgs e)
        {
            Debug.Print(e.Message);
        }

        //public static void MainOLD()
        //{
        //    WebServer webServer = new WebServer();
        //    CommonNetduino.tcpCaller tcpCaller1 = new CommonNetduino.tcpCaller();
        //    tcpCaller1.call();

        //    while (true)
        //    {
        //        int requestLength = webServer.WaitForRequest(new RequestReceivedDelegate(RequestReceived));
        //        if (requestLength > 0)
        //        {
        //            //-- Build response --
        //            responseStr = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
        //            responseStr += ProcessRequest(receivedStr);

        //            // IMPORTANT: SendResponse() sends response AND closes the connection, please call only once per request.
        //            webServer.SendResponse(responseStr);
        //            receivedStr = "";
        //        }
        //        Thread.Sleep(100);
        //    }
        //}

        private static string ProcessRequest(string receivedStr)
        {
            //-- Parse the first line of the request: "GET /sensor/1 HTTP/1.1\r" --
            string firstLine = receivedStr.Substring(0,receivedStr.IndexOf('\n'));
            string[] words = firstLine.Split(' ');
            string[] parts = words[1].Split('/');
            string cmd = parts.Length > 1 ? parts[1] : "";
            string param1 = parts.Length > 2 ? parts[2] : "";
            string param2 = parts.Length > 3 ? parts[3] : "";

            //-- Add more commands and param handling here --
            if (cmd == "sensor" )
            {
                led.Write(param1 == "1");
            }

            //-- Optional string to return to caller --
            return "Command executed for test at " + DateTime.Now.ToString();
        }

        private static void RequestReceived(object param)
        {
            receivedStr += (string)param;
        }
    }
}